﻿using SpiderStudy.Utils.Currency.FileUtils;
using SpiderStudy.Utils.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace SpiderStudy.Utils.Currency
{
    internal class FloderHelper
    {
        /// <summary>
        /// 创建一个文件夹路径
        /// </summary>
        /// <param name="ParentFloder">父路径，以\\结尾</param>
        /// <param name="ChildFloderName">子路径，不需要\\，直接写“FloderName”即可</param>
        /// <param name="CreateFlag">是否创建文件夹</param>
        /// <returns></returns>
        public string CreateFloderPath(string ParentFloder, string ChildFloderName, bool CreateFlag = true)
        {
            if (!(ParentFloder.LastIndexOf("\\") == ParentFloder.Length - 1))
            {
                //如果不以\\结尾，则代表这个路径没有封闭，先进行封闭
                System.Console.WriteLine("父路径未封闭，请注意以\\进行结尾");
                ParentFloder = ParentFloder + "\\";
            }
            ParentFloder = ParentFloder + TextHelper.ReplaceBadChar(ChildFloderName) + "\\";
            if (CreateFlag)
                FloderHelper.FolderCreate(ParentFloder);

            return ParentFloder;
        }

        /// <summary>
        /// 初始化系统根目录
        /// </summary>
        public void InitFloder()
        {
            FloderExits(StaticValue.BinPath, true);
            FloderExits(StaticValue.TempPath, true);
        }

        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <param name="FloderPath"></param>
        public static void OpenFloder(string FloderPath)
        {
            if (TextHelper.JudgeNull(FloderPath))
                return;
            System.Diagnostics.Process.Start(FloderPath);
        }

        /// <summary>
        /// 获取文件夹路径
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string GetFloderPath(String FilePath)
        {
            string Floderpath = "";
            try { Floderpath = Path.GetDirectoryName(FilePath) + "\\"; }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return Floderpath;
        }

        /// <summary>
        /// 每次启动程序清理缓存
        /// </summary>
        /// <param name="FloderPath"></param>
        public static void DeleteAllChildrenFile(String FloderPath)
        {
            foreach (string line in FloderHelper.GetFullFileNameList(FloderPath))
            {
                FileHelper.FileDelete(line);
            }
        }

        /// <summary>
        /// 判断文件夹是否存在
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="CreatFlag"></param>
        /// <returns></returns>
        public static bool FloderExits(String FloderPath, bool CreatFlag)
        {
            if (!Directory.Exists(FloderPath))
            {
                if (!CreatFlag)
                {
                    return false;
                }

                Console.WriteLine("已创建" + FloderPath);
                try { Directory.CreateDirectory(FloderPath); }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                    PrintLog.Log("异常文件夹名字" + FloderPath);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 删除文件夹  包括里面的文件
        /// </summary>
        /// <param name="FloderPath"></param>
        public static void FolderDelete(String FloderPath)
        {
            try
            {
                Directory.Delete(FloderPath, true);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="FloderPath"></param>
        public static void FolderCreate(String FloderPath)
        {
            try
            {
                Directory.CreateDirectory(FloderPath);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 获取目录下文件列表，带路径
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public static List<String> GetFullFileNameList(String FloderPath, String Pattern = "*.*")
        {
            List<String> FileList = new List<String>();
            try
            {
                FileList = Directory.GetFiles(FloderPath, Pattern).ToList<String>();
            }
            catch (Exception ex)
            {
                PrintLog.Log("获取文件列表失败，文件夹{0}没有创建。", FloderPath);
                PrintLog.Log(ex);
            }
            return FileList;
        }

        /// <summary>
        /// 获取文件夹列表
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <returns></returns>
        public static List<string> GetFloderNameList(string FloderPath)
        {
            List<string> FloderList = new List<string>();
            if (!FloderExits(FloderPath, true))
            {
                Console.WriteLine("创建新文件夹失败");
                return FloderList;
            }
            try
            {
                foreach (string SingleFloder in Directory.GetDirectories(FloderPath).ToList<string>())
                {
                    FloderList.Add(SingleFloder + "\\");
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return FloderList;
        }

        /// <summary>
        /// 移动文件夹文件到新文件夹
        /// </summary>
        /// <param name="OldFloder"></param>
        /// <param name="NewFloder"></param>
        public void FloderFileMove(string OldFloder, string NewFloder)
        {
            if (!FloderExits(NewFloder, true))
            {
                Console.WriteLine("创建新文件夹失败");
                return;
            }
            foreach (string line in FloderHelper.GetFullFileNameList(OldFloder))
            {
                new FileHelper().FileMove(line, Path.GetFullPath(NewFloder) + "\\" + Path.GetFileName(line), true);
            }
        }

        /// <summary>
        /// 移动单个文件
        /// </summary>
        /// <param name="OldFilePath"></param>
        /// <param name="NewFilePath"></param>
        private void FileMove(string OldFilePath, string NewFilePath, bool CoverFlag = true)
        {
            try
            {
                if (CoverFlag && FileHelper.FileExits(NewFilePath))
                    FileHelper.FileDelete(NewFilePath);

                File.Move(OldFilePath, NewFilePath);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        /// <summary>
        /// 选择一个文件夹
        /// </summary>
        /// <returns></returns>
        public string SelectFloder()
        {
            using (FolderBrowserDialog SelectFileFolder = new FolderBrowserDialog())
            {
                if (SelectFileFolder.ShowDialog() == DialogResult.OK)
                {
                    return SelectFileFolder.SelectedPath + "\\";
                }
                else
                    return "";
            }
        }
    }
}